package com.zhourh.bang.modular.edu.controller;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.zhourh.bang.common.controller.BaseController;
import com.zhourh.bang.common.persistence.dao.SScoreMapper;
import com.zhourh.bang.common.persistence.model.SScore;
import com.zhourh.bang.common.support.Response;
import com.zhourh.bang.modular.edu.dao.ScoreDao;
import com.zhourh.bang.modular.edu.dto.UpScore;
import com.zhourh.bang.modular.edu.service.IScoreService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 成绩控制器
 *
 * @author fengshuonan
 * @Date 2018-06-22 10:54:25
 */
@Controller
@RequestMapping("/score")
public class ScoreController extends BaseController {

    private String PREFIX = "/edu/score/";

    @Resource
    private SScoreMapper scoreMapper;

    @Resource
    private ScoreDao scoreDao;

    @Resource
    private IScoreService iScoreService;

    /**
     * 跳转到成绩首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "score.html";
    }

    /**
     * 跳转到添加成绩
     */
    @RequestMapping("/score_add")
    public String scoreAdd() {
        return PREFIX + "score_add.html";
    }

    /**
     * 跳转到修改成绩
     */
    @RequestMapping("/score_update/{scoreId}")
    public String scoreUpdate(@PathVariable Integer scoreId, Model model) {
        return PREFIX + "score_edit.html";
    }

    /**
     * 获取成绩列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(Integer pageIndex, Integer pageSize) {
        List<SScore> scoreList = scoreMapper.selectPage(new RowBounds((pageIndex-1)*pageSize,pageSize),null);
        Integer total = scoreMapper.selectCount(null);
        Map<String,Object> map = new HashMap<>();
        map.put("rows",scoreList);
        map.put("total",total);
        return map;
    }

    /**
     * 新增成绩
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add() {
        return super.SUCCESS_TIP;
    }

    /**
     * 删除成绩
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete() {
        return SUCCESS_TIP;
    }


    /**
     * 修改成绩
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update() {
        return super.SUCCESS_TIP;
    }

    /**
     * 成绩详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail() {
        return null;
    }

    /**
     * 上传成绩列表
     */
    @RequestMapping(value = "upload")
    @ResponseBody
    public Response upload(MultipartFile file){
        System.out.println(file.getName());
        System.out.println(file.getSize());
        System.out.println(file.getOriginalFilename());
        int count = 0;
        try {
            ExcelReader reader = ExcelUtil.getReader(file.getInputStream(),0);
//            List<SScore> studentList = reader.readAll(SScore.class);
//            if(studentList!=null && !studentList.isEmpty()){
//                for(SScore score :studentList){
//                    if(score.insert()){
//                        count++;
//                    }
//                }
//            }
            List<UpScore> scoreList = reader.readAll(UpScore.class);
            iScoreService.uploadScore(scoreList);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return Response.success();
    }

    /**
     * 随机生成分数
     */
    @RequestMapping(value = "gen")
    @ResponseBody
    public Object gen(){
        iScoreService.genScore();
        return SUCCESS_TIP;
    }


    /**
     * 获取格式化后的分数数据
     */
    @RequestMapping(value = "getFormatScore")
    @ResponseBody
    public Object getFormatScore(){
        Map<String,Object> result = iScoreService.getFormatScore();
        return result;
    }

    /**
     * 生成.arff文件
     */
    @RequestMapping(value = "genFormatFile")
    @ResponseBody
    public Object genFormatFile(String rule){
        this.iScoreService.genFormatFile("",rule,2);
        return "SUCCESS";
    }

    /**
     * 分析ARFF文件
     */
    @RequestMapping(value = "digData")
    @ResponseBody
    public Object digData(){
        return "SUCCESS";
    }
}
